//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     系统管理业务类库
//     菜单栏业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------

using CL.Enum.Common;
using CL.SystemInfo.DAL;
using CL.SystemInfo.Entity;
using CL.Tools.Common;
using CL.View.Entity.SystemInfo;
using System.Collections.Generic;

namespace CL.SystemInfo.BLL
{

    /// <summary>
    /// NavigationBLL 业务
    /// </summary>
    public class NavigationBLL
    {
        NavigationDAL dal = new NavigationDAL(DbConnectionEnum.CaileSystem);

        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int InsertEntity(NavigationEntity entity)
        {
            return dal.InsertEntity(entity);
        }
        /// <summary>
        /// 修改对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int ModifyEntity(NavigationEntity entity)
        {
            return dal.ModifyEntity(entity);
        }
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelEntity(int id)
        {
            return dal.DelEntity(id);
        }

        /// <summary>
        /// 导航名称是否存在
        /// </summary>
        /// <param name="itemname"></param>
        /// <returns></returns>
        public bool Exists(string itemname)
        {
            return dal.Exists(itemname);
        }
        /// <summary>
        /// 导航名称是否存在
        /// </summary>
        /// <param name="itemname"></param>
        /// <returns></returns>
        public bool Exists(int id)
        {
            return dal.Exists(id);
        }
        /// <summary>
        /// 修改排序号
        /// </summary>
        public bool UpdateSortID(int id, string strValue)
        {
            return dal.UpdateSortID(id, strValue);
        }
        /// <summary>
        /// 查询对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public NavigationEntity QueryEntity(int id)
        {
            return dal.QueryEntity(id);
        }
        /// <summary>
        /// 获取类别列表 (缓存)
        /// </summary>
        /// <param name="ParentId">父ID</param>
        /// <returns>DataTable</returns>
        public List<udv_Navigation> GetListCache(int ParentId)
        {
            string CacheKey = "CL_NavigationDataTable-" + ParentId;
            object objModel = CacheHelper.Get(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetList(ParentId);
                    if (objModel != null)
                    {
                        int ModelCache = ConfigHelper.GetConfigInt("MODELCACHE");
                        CacheHelper.Insert(CacheKey, objModel, ModelCache);
                    }
                }
                catch {
                    throw;
                }
            }
            List<udv_Navigation> ls = new List<udv_Navigation>();
            var entitys = (List<NavigationEntity>)objModel;
            foreach (var entity in entitys)
                ls.Add(new udv_Navigation()
                {
                    ActionType = entity.ActionType,
                    id = entity.id,
                    IsLock = entity.IsLock,
                    IsSys = entity.IsSys,
                    LinkUrl = entity.LinkUrl,
                    Name = entity.Name,
                    ParentID = entity.ParentID,
                    Remark = entity.Remark,
                    SortID = entity.SortID,
                    Title = entity.Title
                });
            return ls;
        }
        /// <summary>
        /// 获取类别列表
        /// </summary>
        /// <param name="parent_id">父ID</param>
        /// <returns>DataTable</returns>
        public List<udv_Navigation> QueryEntitys(int parent_id)
        {
            return dal.QueryEntitys(parent_id);
        }

        /// <summary>
        /// 从内存中取得所有下级类别列表（自身迭代）
        /// </summary>
        public List<udv_Navigation> GetChilds()
        {
            List<udv_Navigation> newData = new List<udv_Navigation>();
            foreach (var item in dal.QueryListByPage())
            {
                
                    //添加一行数据
                    udv_Navigation row = new udv_Navigation();
                    row.id = item.id;
                    row.ParentID = item.ParentID;
                    row.Name = item.Name;
                    row.Title = item.Title;
                    row.LinkUrl = item.LinkUrl;
                    row.SortID = item.SortID;
                    row.IsLock = item.IsLock;
                    row.Remark = item.Remark;
                    row.ActionType = item.ActionType;
                    row.IsSys = item.IsSys;
                    newData.Add(row);
            }        
            return newData;
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<udv_Navigation> QueryListByPage( int pageSize, int pageIndex, ref int recordCount)
        {
            return dal.QueryListByPage(pageSize, pageIndex, ref recordCount);
        }
    }
}
